﻿using BusinessLogicLayer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        CungCapBLL CCBLL = new CungCapBLL();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // ShowHideButton(true);
            //ShowHideTxt(false);
            dataGridView1.DataSource = CCBLL.LayTatCa();

        }
        private void ShowHideButton(bool sh)
        {
            btnTim.Enabled = sh;
            btnThem.Enabled = sh;
            btnLuu.Enabled = !sh;
            btnXoa.Enabled = sh;
        }
        private void ShowHideTxt(bool sh)
        {
            txtMaNCC.Enabled = sh;
            txtTenNCC.Enabled = sh;
            txtDiaChi.Enabled = sh;
            txtDienThoai.Enabled = sh;
            txtEmail.Enabled = sh;
            txtThanhPho.Enabled = sh;
        }
        private void ClearTxt()
        {
            txtMaNCC.Clear();
            txtTenNCC.Clear();
            txtDiaChi.Clear();
            txtDienThoai.Clear();
            txtEmail.Clear();
            txtThanhPho.Clear();
            txtMaNCC.Focus();
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            txtMaNCC.Text = dataGridView1[0, row].Value.ToString();
            txtTenNCC.Text = dataGridView1[1, row].Value.ToString();
            txtDiaChi.Text = dataGridView1[2, row].Value.ToString();
            txtDienThoai.Text = dataGridView1[3, row].Value.ToString();
            txtThanhPho.Text = dataGridView1[4, row].Value.ToString();
            txtEmail.Text = dataGridView1[5, row].Value.ToString();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            // ShowHideButton(false);
            // ShowHideTxt(true);
            ClearTxt();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            string ma = txtMaNCC.Text;
            string ten = txtTenNCC.Text;
            string diachi = txtDiaChi.Text;
            string dienthoai = txtDienThoai.Text;
            string thanhpho = txtThanhPho.Text;
            string email = txtEmail.Text;
            if (txtMaNCC.Text.Equals("") || txtTenNCC.Text.Equals("") || txtDiaChi.Text.Equals("") || txtDienThoai.Text.Equals("") || txtEmail.Text.Equals("") || txtThanhPho.Text.Equals(""))
            {
                MessageBox.Show("Bạn Chưa Nhập Đầy Đủ Thông Tin.");
            }
            else
            {
                MessageBox.Show(CCBLL.insert(ma, ten, diachi, dienthoai, thanhpho, email));
                CCBLL.insert(txtMaNCC.Text, txtTenNCC.Text, txtDiaChi.Text, txtDienThoai.Text, txtEmail.Text, txtThanhPho.Text);
                dataGridView1.DataSource = CCBLL.LayTatCa();
                ShowHideTxt(false);
                ShowHideButton(true);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            CCBLL.delete(txtMaNCC.Text);
            dataGridView1.DataSource = CCBLL.LayTatCa();
            ClearTxt();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            ClearTxt();
            btnGo.Visible = true;
            txtMaNCC.Focus();

        }

        private void btnGo_Click(object sender, EventArgs e)
        {

            btnGo.Visible = false;
            string mncc = txtMaNCC.Text;
            if (mncc.Equals("") == false)
            {
                try
                {
                    dataGridView1.DataSource = CCBLL.search(txtMaNCC.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Lỗi: " + ex);
                }

            }
            else
            {
                MessageBox.Show("Vui lòng nhập mã nhà cung cấp cần tìm!");
            }
        }

        private void txtMaNCC_TextChanged(object sender, EventArgs e)
        {
            this.AcceptButton = btnGo;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn thoát?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
    

